package com.axjy.controller;

import com.axjy.pojo.Permission;
import com.axjy.pojo.Role;
import com.axjy.service.PermissionService;
import com.axjy.service.RoleService;
import com.axjy.util.Result;
import com.axjy.vo.TreeNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.swing.*;
import java.util.List;

@RestController
@RequestMapping("sys/role")
@Slf4j
public class RoleController {

    @Resource
    private RoleService roleService;

    @Resource
    private PermissionService permissionService;

    private String msg;

    @GetMapping("/list")
    public Result list(){
        List<Role> list= roleService.list();
        return Result.success(list);
    }

    @GetMapping("/search4Tree")
    public Result search4Tree(Integer roleId){
        List<TreeNode> nodes=permissionService.search4Tree(roleId);
        return Result.success(nodes);
    }

    @PostMapping("/grant")
    public Result grant(Integer roleId,Integer[] ids){
        try{
            permissionService.grant(roleId,ids);
            return Result.success("角色授权成功!");
        }catch (Exception ex){
            msg="角色授权失败!";
            log.error(msg,ex);
            return Result.fail(msg);
        }
    }

}
